using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.BaseManage.Dto;
using ZR.Model.BaseManage;
using ZR.Repository;
using ZR.Service.BaseManage.IBaseManageService;
using Infrastructure;
using Mapster;

namespace ZR.Service.BaseManage
{
    /// <summary>
    /// plc信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IBasePlcService), ServiceLifetime = LifeTime.Transient)]
    public class BasePlcService : BaseService<BasePlc>, IBasePlcService
    {
        /// <summary>
        /// 查询plc信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<BasePlcDto> GetPageList(BasePlcQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<BasePlc, BasePlcDto>(parm);

            return response;
        }

        /// <summary>
        /// 查询plc信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<BasePlcDto> GetList(BasePlcQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
               .ToList().Adapt<List<BasePlcDto>>();

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="PlcCode"></param>
        /// <returns></returns>
        public BasePlc GetInfo(string PlcCode)
        {
            var response = Queryable()
                .Where(x => x.PlcCode == PlcCode)
                .First();

            return response;
        }

        /// <summary>
        /// 添加plc信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public BasePlc AddBasePlc(BasePlc model)
        {
            if (this.Any(it => it.PlcCode == model.PlcCode))
            {
                throw new CustomException($"系统中已包含{model.PlcCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改plc信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBasePlc(BasePlc model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 删除plc信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBasePlc(string[] ids)
        {
            if (this.Context.Queryable<BasePlcTag>().Any(it => ids.Contains(it.PlcCode)))
            {
                throw new CustomException($"系统中已经存在此PLC的标签信息，不能删除");
            }
            return Delete(ids);
        }


        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<BasePlc> QueryExp(BasePlcQueryDto parm)
        {
            var predicate = Expressionable.Create<BasePlc>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlcCode), it => it.PlcCode == parm.PlcCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlcName), it => it.PlcName == parm.PlcName);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlcType), it => it.PlcType == parm.PlcType);
            return predicate;
        }
    }
}